package ks.packs.anotherDay;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LinkGetter {

	private Pattern htmltag;
	private Pattern link;

	String Content;

	public LinkGetter(String pContent) {

		Content = pContent;
		htmltag = Pattern.compile("<a\\b[^>]*href=\"[^>]*\\D\\D3\">(.*?)</a>");
		link = Pattern.compile("href=\"[^\"]*");

	}

	public List<String> getLinks(String url) {

		ksLog.d("url", url);

		List<String> links = new ArrayList<String>();

		try {

			BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(new URL(url).openStream()));
			String s;
			StringBuilder builder = new StringBuilder();

			while ((s = bufferedReader.readLine()) != null) {

				builder.append(s);
			}

			Matcher tagmatch = htmltag.matcher(builder.toString());

			while (tagmatch.find()) {

				ksLog.d("match", tagmatch.group().toString());

				Matcher matcher = link.matcher(tagmatch.group());
				matcher.find();

				String link = matcher.group().replaceFirst("href=\"", "")
						.replaceFirst("\">", "");

				if (valid(link)) {

					ksLog.d("link", link);

					links.add(link);
				}

			}

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return links;

	}

	private boolean valid(String s) {
		if (s.matches("javascript:.*|mailto:.*")) {
			return false;
		}
		return true;
	}

	private String makeAbsolute(String url, String link) {
		if (link.matches("http://.*")) {
			return link;
		}
		if (link.matches("/.*") && url.matches(".*$[^/]")) {
			return url + "/" + link;
		}
		if (link.matches("[^/].*") && url.matches(".*[^/]")) {
			return url + "/" + link;
		}
		if (link.matches("/.*") && url.matches(".*[/]")) {
			return url + link;
		}
		if (link.matches("/.*") && url.matches(".*[^/]")) {
			return url + link;
		}
		throw new RuntimeException("Cannot make the link absolute. Url: " + url
				+ " Link " + link);
	}
}
